package com.smcdd.cdb.dbo.cdb_dbo.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.app.App;
import com.smcdd.cdb.dbo.cdb_dbo.network.HttpObserver;
import com.smcdd.cdb.dbo.cdb_dbo.network.factory.RetrofitFactory;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.BaseResponseEntity;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.MainTenance;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Tain_v2;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.NetWorkCodeInfo;
import com.smcdd.cdb.dbo.cdb_dbo.ui.adapter.*;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BasePresenter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DateUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.adapter.TainAdapter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainEncry;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainUdis;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainVersion;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.TainDialog;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 作者：Administrator on 2017/8/8 14:06
 * 邮箱：zhanghuaiha@gmail.com
 */

public class MaintainFiltrateActivity extends BaseActivity {


    private TextView mDialog,title;
    private ListView listView;
    private ImageView net_back;
    private EditText search;
    private TextView yet_search_tv;
    private List<Tain_v2> data;
    private String name;
    private String location;
    private List<Tain_v2> filterDateList;
    private RecyclerView recyclerView;
    private TainAdapter tainAdapter;
    private String date;
    private String status;
    private TainDialog listDialog;
    private String udid;
    private String versionName;
    private String id;
    private boolean flags;

    @Override
    public void initView() {
        mDialog = (TextView) findViewById(R.id.wx_fg_contact_dialog);
        title = (TextView) findViewById(R.id.title);
        listView = (ListView) findViewById(R.id.wx_fg_contact_lv);
        net_back = (ImageView) findViewById(R.id.net_back);
        search = (EditText) findViewById(R.id.yet_search);
        yet_search_tv = (TextView) findViewById(R.id.yet_search_tv);
        recyclerView = (RecyclerView) findViewById(R.id.maintain_recycler);
    }

    @Override
    public void initData() {
        versionName = ObtainVersion.getVersionName(this);
        id = App.SP.getString("id", "");
        udid = App.SP.getString("udid", "");
        data = (List<Tain_v2>) getIntent().getSerializableExtra("list");
        date = getIntent().getStringExtra("date");
        status = getIntent().getStringExtra("status");
        flags = getIntent().getBooleanExtra("flags",false);

        title.setText("待维护");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        setAdapter();
        tainAdapter.visible2gone(flags);
        tainAdapter.addData(data);
    }


    private void setAdapter() {
        if (tainAdapter == null) {
            tainAdapter = new TainAdapter(this);
        }
        recyclerView.setAdapter(tainAdapter);
        tainAdapter.setItemClickListenter((dataBean, position,flag,i) -> {
            long l = 0;
            if (status.equals("1")) {
                loadDiaolog(date,dataBean.getCharging_info_ids());
            }else{
                if (this.date!= null&&!this.date.equals("")) {
                    long time_long = DateUtils.getTime_long(date);
                    l = time_long - 86400000;
                }else {
                    l = System.currentTimeMillis() - 86400000;
                }
                String time = DateUtils.getTime(l);
                loadDiaolog(time,dataBean.getCharging_info_ids());
            }

        });
    }

    /**
     * 获取dialog数据
     * @param date
     * @param charging_info_ids
     */
    public void loadDiaolog(String date,String charging_info_ids){
        String time = System.currentTimeMillis() + "";
        Map<String,String> map = new LinkedHashMap<>();
        map.put("rf","2");
        map.put("ts",time);
        map.put("v",versionName);
        map.put("i", NetWorkCodeInfo.MAINTENANCE_CHARGING);
        map.put("udid",udid);
        map.put("eid", id);
        map.put("date",date);
        map.put("charging_info_ids",charging_info_ids);
        String encry1 = ObtainEncry.getEncry(map);
        map.put("encry",encry1);
        RetrofitFactory.getmInstanse().mApi.getmaintenance(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseResponseEntity<List<MainTenance>>>() {
                    @Override
                    public void success(BaseResponseEntity<List<MainTenance>> mainTenanceBaseResponseEntity) {
                        if (mainTenanceBaseResponseEntity.code == 200) {
                            List<MainTenance> data = mainTenanceBaseResponseEntity.data;
                            Dialog(data);
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.showToast(MaintainFiltrateActivity.this.getString(R.string.error_date));
                    }
                });
    }

    public void Dialog( List<MainTenance> data) {
        listDialog = new TainDialog(MaintainFiltrateActivity.this,data,"2");
        listDialog.show();
    }




    /**
     * 搜索
     * @param tv
     */
    public void searh(String tv){
        if (filterDateList == null) {
            filterDateList= new ArrayList<>();
        }else {
            filterDateList.clear();
        }

        if (tv.equals("取消")) {
            search.setText("");
            filterDateList.addAll(data);
        }else {
            String s = search.getText().toString().trim();
            if (TextUtils.isEmpty(s)) {
                filterDateList.addAll(data);
            }else {
                if (!data.contains(s)) {
                    for (Tain_v2 info : data) {
                        name = info.getStore_info().getName();
                        location = info.getStore_info().getArea_name()+info.getStore_info().getSpecific_address();
                        if (name.indexOf(s) != -1 || location.indexOf(s) != -1) {
                            filterDateList.add(info);
                        }
                    }
                }
            }
        }
        tainAdapter.addData(filterDateList);
    }


    @Override
    public void initListener() {
        net_back.setOnClickListener(v -> finish());
        yet_search_tv.setOnClickListener(v -> {
            searh(yet_search_tv.getText().toString());
            if (yet_search_tv.getText().toString().equals("搜索")) {
                yet_search_tv.setText("取消");
            }else {
                yet_search_tv.setText("搜索");
            }

        });
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_filtrate;
    }
}
